#include "guicontainerform.h"
#include "ui_guicontainerform.h"
#include "homepage.h"
#include "stockpage.h"
#include "commodityinformation.h"
#include <QDebug>
#include <QStackedWidget>
#include <QLayout>
#include <QMessageBox>

#include "purchase.h"
#include "stockremind.h"
#include "commoditysale.h"
#include "addmodifysupplier.h"
#include "addmodifycommodity.h"
#include "supplier.h"
#include "categorysettings.h"
#include "memberprofile.h"
#include "setmemberlevel.h"
#include <QDesktopWidget>
#include "handlemembercard.h"
#include "membercardrecharge.h"
#include "staffresult.h"
#include "modifyloginpassword.h"
#include "systemsettings.h"
#include "staffmanagement.h"
#include "memberoperationdetails.h"
#include "salesdetails.h"
#include "inquiresql.h"

GuiContainerForm::GuiContainerForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GuiContainerForm)
{
    ui->setupUi(this);
//    this->setLayout(ui->mainLayout);
    m_StackedWidget = new QStackedWidget;
    QHBoxLayout *MainLayout = new QHBoxLayout;
    ui->toolBox->setFixedWidth(152);
    MainLayout->addWidget(m_StackedWidget);
    MainLayout->addWidget(ui->toolBox);
    this->setLayout(MainLayout);   

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    ui->toolBox->setCurrentIndex(0);

    eliminationPushButtonBorder();

    m_homepage = new HomePage;
    m_stockpage = new StockPage;
    m_commodityinformation = new CommodityInformation;
//    m_newcommodity = new NewCommodity;
//    m_attributesettings = new AttributeSettings;
    m_purchase = new Purchase;
    m_stockremind = new StockRemind(this);
    m_commoditysale = new CommoditySale;
//    m_supplier = new Supplier;
    m_addModifySupplier = new AddModifySupplier;
//    m_categorySettings = new CategorySettings;
    m_addModifyCommodity = new AddModifyCommodity;
    m_memberProfile = new MemberProfile;
    m_setMemberLevel = new SetMemberLevel(this);
    m_handleMemberCard = new HandleMemberCard(this);
    m_memberCardRecharge = new MemberCardRecharge(this);
    m_staffResult = new StaffResult;
    m_modifyLoginPassword = new ModifyLoginPassword(this);
    m_systemSettings = new SystemSettings(this);
    m_staffManagement = new StaffManagement;
    m_memberOperationDetails = new MemberOperationDetails;
    m_salesDetails = new SalesDetails;

    m_setMemberLevel->hide();
    m_handleMemberCard->hide();
    m_memberCardRecharge->hide();
    m_modifyLoginPassword->hide();
    m_systemSettings->hide();
    m_stockremind->hide();

//    pagesWidget = new QStackedWidget(this);
    m_StackedWidget->addWidget(m_homepage);
    m_StackedWidget->addWidget(m_stockpage);
    m_StackedWidget->addWidget(m_commodityinformation);
    m_StackedWidget->addWidget(m_purchase);
    m_StackedWidget->addWidget(m_stockremind);
    m_StackedWidget->addWidget(m_commoditysale);
    m_StackedWidget->addWidget(m_memberProfile);
    m_StackedWidget->addWidget(m_staffResult);
    m_StackedWidget->addWidget(m_staffManagement);
    m_StackedWidget->addWidget(m_memberOperationDetails);
    m_StackedWidget->addWidget(m_salesDetails);

    m_StackedWidget->setCurrentWidget(m_homepage);        //初始化显示首页

    connect(m_homepage, SIGNAL(PushButtonResponse(QString)),
            this, SLOT(slotChangePage(QString)));
//    connect(m_commodityinformation, SIGNAL(comboBoxOtherSettingsChange(QString)),
//            m_attributesettings, SLOT(slotChangestackedWidgetAttribute(QString)));
//    connect(m_attributesettings, SIGNAL(signalUpdateComboBoxAttribute(int)),
//            m_commodityinformation, SLOT(slotUpdateAddModifyCommodity(int)));
    connect(m_setMemberLevel, SIGNAL(updateMemberLevel()),
            m_handleMemberCard, SLOT(slotUpdateMemberLevel()));
    connect(m_setMemberLevel, SIGNAL(updateMemberLevel()),
            m_memberProfile, SLOT(slot_updateMemberProfile()));
    connect(m_systemSettings, SIGNAL(updateSystemSettingsInformation()),
            m_commoditysale, SLOT(slotUpdateSystemSettingsInformation()));
    connect(m_systemSettings, SIGNAL(modifySystemInformationSuccess()),
            this, SLOT(slotModifySystemInformationSuccess()));
    connect(m_commoditysale, SIGNAL(returnHomePage()),
            this, SLOT(slotReturnHomePage()));
}

GuiContainerForm::~GuiContainerForm()
{
    delete ui;
}

void GuiContainerForm::slotChangePage(QString pagename)
{
    if (pagename == "stockpage")
        m_StackedWidget->setCurrentWidget(m_stockpage);
    if (pagename == "homepage")
        m_StackedWidget->setCurrentWidget(m_homepage);
    if (pagename == "purchasepage")
        m_StackedWidget->setCurrentWidget(m_purchase);
    if (pagename == "commoditySalesPage")
        m_StackedWidget->setCurrentWidget(m_commoditysale);
}

void GuiContainerForm::slotModifySystemInformationSuccess()
{
    QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                         tr("\n修改系统设置成功!"));
}

void GuiContainerForm::slotReturnHomePage()
{
    m_StackedWidget->setCurrentWidget(m_homepage);
}

void GuiContainerForm::on_pushButtonCurrentStock_clicked()
{
    m_StackedWidget->setCurrentWidget(m_stockpage);
}

void GuiContainerForm::on_pushButtonCommoditySales_clicked()
{
    m_StackedWidget->setCurrentWidget(m_commoditysale);
    m_commoditysale->setFocus();
}

void GuiContainerForm::on_pushButtonCommodityInformation_clicked()
{
    m_commodityinformation->displayCommodityInformation("all");
    m_StackedWidget->setCurrentWidget(m_commodityinformation);    
}

void GuiContainerForm::on_pushButtonPurchase_clicked()
{
    m_StackedWidget->setCurrentWidget(m_purchase);
}

void GuiContainerForm::on_pushButtonStockPrompt_clicked()
{
    m_StackedWidget->setCurrentWidget(m_homepage);

    m_stockremind->setWindowFlags(Qt::Dialog);
    m_stockremind->move((QApplication::desktop()->width() - m_stockremind->width())/2,
                     (QApplication::desktop()->height() - m_stockremind->height())/2);
    //在对 QWidget 操作时，不能操作其他东西
    m_stockremind->setWindowModality(Qt::ApplicationModal);
    m_stockremind->setAttribute(Qt::WA_ShowModal, true);
    m_stockremind->displayStockRemind();
    m_stockremind->show();
}

void GuiContainerForm::eliminationPushButtonBorder()
{
    //消除边框
    ui->pushButtonCommoditySales->setFlat(true);
    ui->pushButtonSalesDetail->setFlat(true);
    ui->pushButtonModifyPassword->setFlat(true);
    ui->pushButtonAdjustmentdate->setFlat(true);
    ui->pushButtonMemberRechargeDetail->setFlat(true);
    ui->pushButtonRevenueExpenditureManagement->setFlat(true);

    ui->pushButtonMemberChangingCard->setFlat(true);
    ui->pushButtonMemberChargeMoney->setFlat(true);
    ui->pushButtonMemberLevel->setFlat(true);
    ui->pushButtonMemberHandled->setFlat(true);
    ui->pushButtonMemberProfile->setFlat(true);

    ui->pushButtonCommodityInformation->setFlat(true);
    ui->pushButtonPurchase->setFlat(true);
    ui->pushButtonStockAdjustment->setFlat(true);
    ui->pushButtonStockInventory->setFlat(true);
    ui->pushButtonCurrentStock->setFlat(true);
    ui->pushButtonPurchaseSaleSave->setFlat(true);
    ui->pushButtonStockPrompt->setFlat(true);

    ui->pushButtonMemberConsumeRanking->setFlat(true);
    ui->pushButtonCommoditySalesRanking->setFlat(true);
    ui->pushButtonCommodityReturnedPurchaseRanking->setFlat(true);
    ui->pushButtonStaffResultsRanking->setFlat(true);
    ui->pushButtonProfitStatementAnalysis->setFlat(true);
    ui->pushButtonTrendAnalysisReport->setFlat(true);
    ui->pushButtonProfitLossAnalysisReport->setFlat(true);

    ui->pushButtonSystemInit->setFlat(true);
    ui->pushButtonStaffManagement->setFlat(true);
    ui->pushButtonSystemSettings->setFlat(true);
    ui->pushButtonUserCompetence->setFlat(true);

    ui->pushButtonSystemRegistration->setFlat(true);
}

void GuiContainerForm::on_pushButtonMemberProfile_clicked()
{
    m_memberProfile->displayMemberProfile("all");
    m_StackedWidget->setCurrentWidget(m_memberProfile);
}

void GuiContainerForm::on_pushButtonMemberLevel_clicked()
{
    m_StackedWidget->setCurrentWidget(m_homepage);

    m_setMemberLevel->move((QApplication::desktop()->width() - m_setMemberLevel->width())/2,
                     (QApplication::desktop()->height() - m_setMemberLevel->height())/2);
    m_setMemberLevel->setWindowFlags(Qt::Dialog);
    //在对 QWidget 操作时，不能操作其他东西
    m_setMemberLevel->setWindowModality(Qt::ApplicationModal);
    m_setMemberLevel->setAttribute(Qt::WA_ShowModal, true);
    m_setMemberLevel->displayMemberLevel();
    m_setMemberLevel->exec();    
}

void GuiContainerForm::on_pushButtonMemberHandled_clicked()
{
    m_StackedWidget->setCurrentWidget(m_homepage);

    m_handleMemberCard->move((QApplication::desktop()->width() - m_handleMemberCard->width())/2,
                     (QApplication::desktop()->height() - m_handleMemberCard->height())/2);
    m_handleMemberCard->setWindowFlags(Qt::Dialog);
    //在对 QWidget 操作时，不能操作其他东西
    m_handleMemberCard->setWindowModality(Qt::ApplicationModal);
    m_handleMemberCard->setAttribute(Qt::WA_ShowModal, true);
    m_handleMemberCard->set_autoAddMemberCardNumber(
                m_memberProfile->get_autoAddMemberCardNumber());
    m_handleMemberCard->set_staffName();
    m_handleMemberCard->show();
}

void GuiContainerForm::on_pushButtonMemberChargeMoney_clicked()
{
    m_StackedWidget->setCurrentWidget(m_homepage);

    m_memberCardRecharge->move((QApplication::desktop()->width() - m_memberCardRecharge->width())/2,
                     (QApplication::desktop()->height() - m_memberCardRecharge->height())/2);
    m_memberCardRecharge->setWindowFlags( Qt::Dialog );
    //在对 QWidget 操作时，不能操作其他东西
    m_memberCardRecharge->setWindowModality(Qt::ApplicationModal);
    m_memberCardRecharge->setAttribute(Qt::WA_ShowModal, true);
    m_memberCardRecharge->set_staffName();
    m_memberCardRecharge->initRecharge();
    m_memberCardRecharge->initMemberInformation();
    m_memberCardRecharge->show();
}

void GuiContainerForm::on_pushButtonStaffResultsRanking_clicked()
{
    m_staffResult->displayTableWidgetStaffResult();
    m_StackedWidget->setCurrentWidget(m_staffResult);
}

void GuiContainerForm::on_pushButtonModifyPassword_clicked()
{
    m_StackedWidget->setCurrentWidget(m_homepage);

    m_modifyLoginPassword->move((QApplication::desktop()->width() - m_modifyLoginPassword->width())/2,
                     (QApplication::desktop()->height() - m_modifyLoginPassword->height())/2 - 60);
    m_modifyLoginPassword->setWindowFlags( Qt::Dialog );
    //在对 QWidget 操作时，不能操作其他东西
    m_modifyLoginPassword->setWindowModality(Qt::ApplicationModal);
    m_modifyLoginPassword->setAttribute(Qt::WA_ShowModal, true);
    m_modifyLoginPassword->show();
}

void GuiContainerForm::on_pushButtonSystemSettings_clicked()
{
    m_StackedWidget->setCurrentWidget(m_homepage);

    m_systemSettings->move((QApplication::desktop()->width() - m_systemSettings->width())/2,
                     (QApplication::desktop()->height() - m_systemSettings->height())/2);
    m_systemSettings->setWindowFlags( Qt::Dialog );
    //在对 QWidget 操作时，不能操作其他东西
    m_systemSettings->setWindowModality(Qt::ApplicationModal);
    m_systemSettings->setAttribute(Qt::WA_ShowModal, true);
    m_systemSettings->displaySystemSettingsInformation();
    m_systemSettings->show();
}

void GuiContainerForm::on_pushButtonStaffManagement_clicked()
{
    m_staffManagement->slotDisplayStaffInformation();
    m_StackedWidget->setCurrentWidget(m_staffManagement);
}

void GuiContainerForm::on_pushButtonMemberRechargeDetail_clicked()
{
    m_StackedWidget->setCurrentWidget(m_memberOperationDetails);
}

void GuiContainerForm::on_pushButtonSalesDetail_clicked()
{
    m_StackedWidget->setCurrentWidget(m_salesDetails);
}
